<?php
/**
 * This file contains the mysql db class
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: database.class.php 115 2010-08-12 09:36:12Z sruttloff $
 * @link      phpmy
 */
require_once dirname(__FILE__) . '/database/mysql.class.php';
/**
 * DB Access to MySql
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class database extends mysql
{

    /**
     * database connection encoding
     * @var string default is utf-8
     */
    protected $CONNECTION_ENCODING = 'utf8';
    /**
     * @var object instance
     */
    protected static $INSTANCE = array();
    /**
     * @var string selected database name
     */
    protected $DBNAME = null;
    /**
     * @var bool debuging
     */
    protected $DEBUG = false;
    /**
     * instantiate
     *
     * @return void
     */
    protected function __construct()
    {
    }
    /**
     * return a instance of object
     *
     * @param string $dbname db instance name (optional)
     *
     * @return object instance
     */
    public static function getInstance($dbname = 'default')
    {
        if (!isset(self::$INSTANCE[$dbname]))
        {
            $c = __CLASS__;
            self::$INSTANCE[$dbname] = new $c;
        }
        return self::$INSTANCE[$dbname];
    }
    /**
     * enables query debugging
     *
     * @return void
     */
    public function enableDebugging()
    {
        $this->DEBUG = true;
    }
    /**
     * Connect to db
     *
     * @param string $host  hostname or IP
     * @param string $user  username
     * @param string $name  db name
     * @param string $pass  password
     * @param bool   $quiet suppress error trigger
     *
     * @return bool succ. connect returns true
     */
    public function connect($host = "", $user = "", $name = "", $pass = "", $quiet = false)
    {
        if (!$host && !$user)
        {
            $frontend = frontend::getInstance();
            $config = $frontend->getConfig();
            $host = $config->get('DBSERVER');
            $user = $config->get('DBUSER');
            $name = $config->get('DBBASE');
            $pass = $config->get('DBPASS');
        }
        $this->DBNAME = $name;
        if (!($this->link_id = @mysql_connect($host, $user, $pass)))
        {
            $this->status = "<b>Database Server</b> Connection failed";
            return false;
        }
        if (!mysql_select_db($this->DBNAME, $this->link_id))
        {
            $this->status = "Database <em>" . $this->DBNAME . "</em> could not be selected!";
            if (!$quiet)
                trigger_error($this->status, E_USER_ERROR);
            return false;
        }
        $this->open = true;
        if ($this->CONNECTION_ENCODING)
            $this->query('SET NAMES "' . $this->CONNECTION_ENCODING . '"');
        return true;
    }
}